package com.helloandroid.base

import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import com.helloandroid.tools.MyLog

abstract class BaseActivity: AppCompatActivity() {
    lateinit var mContext:Context
    override fun onCreate(savedInstanceState: Bundle?) {
        // 1. 设置状态栏透明
        // 2. 设置全屏,让背景图片可以填充状态栏
        setStatusBarColor(this, android.R.color.transparent)
        setAndroidNativeLightStatusBar(this, isDarkMode())

//        if (savedInstanceState != null && clearFragmentsTag()){
//            // 销毁时不保存fragment状态
//            savedInstanceState.remove()
//        }

        super.onCreate(savedInstanceState)
        setContentView(getContentViewResId())
        supportActionBar?.hide()
        process()
        mContext = this
        initView()
        initData()
    }

    abstract fun getContentViewResId():Int
    abstract fun initView()
    abstract fun initData()

    fun findBtn(id: Int) = findViewById<View>(id)

    private fun process() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val content = (findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0)
                if (content != null && !isUseFullScreenMode()) {
                    content.fitsSystemWindows = true
                }
            }
        }catch (e: Exception){
            MyLog.elog("设置全屏错误???!!!")
            e.printStackTrace()
        }
    }

    //  是否使用全屏模式
    open fun isUseFullScreenMode() = true
    open fun isDarkMode() = true

    /**
     * 来自于:https://www.jianshu.com/p/7392237bc1de
     * 修改状态栏颜色，支持4.4以上版本
     * @param activity
     * @param colorId
     */
    private fun setStatusBarColor(activity: Activity, colorId: Int) {
        val window: Window = activity.window
        // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = activity.resources.getColor(colorId, null)
    }

    private fun setAndroidNativeLightStatusBar(activity: Activity, dark: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val controller = activity.window.insetsController
            if (controller != null) {
                controller.setSystemBarsAppearance(if (!dark) WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS else 0,
                        WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
                controller.setSystemBarsAppearance(if (!dark) WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS else 0,
                        WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS)
            }
        }else{
            val decor: View = activity.window.decorView
            if (dark) {
                decor.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                decor.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            }
        }
    }

//    public open fun clearFragmentsTag(): Boolean {
//        return true
//    }
}